package btanalyzer.db.dao;

import java.util.List;

import javax.persistence.TypedQuery;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import btanalyzer.db.model.Torrent;

@Component
public class TorrentDaoImpl extends GenericDaoAbstract<Torrent, Long> implements TorrentDao {

	@Override
	@Transactional
	public List<Torrent> selectByMetadataIsNull() {
		TypedQuery<Torrent> q = getEntityManager().createQuery("select t from " + 
				Torrent.class.getName() + " t where t.metadata is null", Torrent.class);
		return q.getResultList();
	}

	@Override
	@Transactional
	public long countByMetadataIsNotNull() {
		TypedQuery<Long> q = getEntityManager().createQuery("select count(t) from " + 
				Torrent.class.getName() + " t where t.metadata is not null", Long.class);
		return q.getSingleResult();
	}

	@Override
	@Transactional
	public Torrent selectById(long id) {
		TypedQuery<Torrent> q = getEntityManager().createQuery("select t from " + 
				Torrent.class.getName() + " t where t.id = :id", Torrent.class);
		q.setParameter("id", id);
		return q.getSingleResult();
	}
}
